package com.bdit.ydcb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bdit.ydcb.R;
import com.bdit.ydcb.model.AreaTask;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shengpj on 2017/7/3.
 */

public class AreaTaskAdapter extends RecyclerView.Adapter<AreaTaskAdapter.ViewHolder>{
    private Context mContext;
    private List<AreaTask> dataList = new ArrayList<>();
    private ItemClickListener itemClickListener;

    public void addAllData(List<AreaTask> dataList) {
        this.dataList.addAll(dataList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.dataList.clear();
    }

    public AreaTaskAdapter(Context context) {
        mContext = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView taskCode;
        public TextView taskName;
        public TextView total;
        public  TextView todoNum;
        public ViewHolder(View itemView) {
            super(itemView);
            taskCode = (TextView) itemView.findViewById(R.id.areaTask_code);
            taskName = (TextView) itemView.findViewById(R.id.areaTask_name);
            total = (TextView) itemView.findViewById(R.id.areaTask_total);
            todoNum = (TextView) itemView.findViewById(R.id.areaTask_todoNum);
            itemView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    itemClickListener.onItemClick(v,getPosition());
                }
            });
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_area_task2, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.taskCode.setText(dataList.get(position).getCode());
        holder.taskName.setText(dataList.get(position).getName());
        holder.total.setText(dataList.get(position).getTotal()+"");
        holder.todoNum.setText(dataList.get(position).getTodoNum()+"");
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public List<AreaTask> getDataList() {
        return dataList;
    }

    public interface ItemClickListener{
        public void onItemClick(View view,int position);
    }
}
